#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "AD.h"
#include "dht11.h"
#include "mq135.h"
#include <math.h>
#include "serial.h"
#include "servo.h"
#include "string.h"
#include "Key.h"
#include "esp8266.h"
#include "onenet.h"
DHT11_Data_TypeDef DHT11_Data;
uint8_t KeyNum;			//定义用于接收键码的变量
float Angle;

uint16_t AD0;	//定义AD值变量
uint16_t vol,vol1;
int ppm,n,k,m;
const char *topics[] = {"/peteat/sub"};
const char pubtopics[] = {"/peteat/pub"};
char pubbud[256];
int main(void)
{
unsigned short timeCount = 0;	//发送间隔变量
unsigned char *dataPtr = NULL;
	
OLED_Init();				
AD_Init();	
Usart1_Init(115200);
Usart2_Init(115200);
Servo_Init();
Key_Init();	
ESP8266_Init();
DHT11_GPIO_Config();
OLED_ShowString(1, 1, "temp:");
OLED_ShowString(2, 1, "humidity:");
OLED_ShowString(3, 1, "ppm:");
while(OneNet_DevLink())			//接入OneNET
Delay_ms(5);	
OneNet_Subscribe(topics, 1);
	while (1)
	{GPIO_ResetBits(GPIOB, GPIO_Pin_12);
		Angle=0;
		int result=0;
		Servo_SetAngle(Angle);
		int test=GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12);
		if(timeCount%40==0){
		if(Read_DHT11(&DHT11_Data) == SUCCESS)
		{
			OLED_ShowNum(2, 10, DHT11_Data.humi_int, 2);
			OLED_ShowString(2, 12, ".");
			OLED_ShowNum(2, 13, DHT11_Data.humi_deci, 2);
			
			OLED_ShowNum(1, 6, DHT11_Data.temp_int, 2);
			OLED_ShowString(1, 8, ".");
			OLED_ShowNum(1, 9, DHT11_Data.temp_deci, 1);
			Serial_Printf("温度: %d.%d  湿度: %d.%d  ",DHT11_Data.temp_int,DHT11_Data.temp_deci,DHT11_Data.humi_int,DHT11_Data.humi_deci);
		}
		if(AD_GetValue(ADC_Channel_0))
		{
		AD0 = AD_GetValue(ADC_Channel_0);		//单次启动ADC，转换通道0
		vol=(AD0*3.3/4096);
		vol1=((AD0*33/4096)%10);
		n=(6.467*10*vol)/(5-vol);
		k=1.0/0.3962;
		m=(uint16_t)AD0*300/255;
    ppm=pow(n,k); 
		OLED_ShowNum(3, 5, ppm,3);
		Serial_Printf("ppm值: %d\r\n", ppm);
		}
	}
		KeyNum = Key_GetNum();			//获取按键键码
		if (KeyNum == 1||test==1)				//按键1按下
		{
			Angle = 30;
			result=1;			
			Servo_SetAngle(Angle);
			Serial_Printf("angle值: %d\r\n", result);	
			UsartPrintf(USART_DEBUG, "OneNet_Publish\r\n");
			sprintf(pubbud,"{\"tem\": %d.%d,\"hum\": %d.%d,\"ppm\": %d,\"servo\": %d}",DHT11_Data.temp_int,DHT11_Data.temp_deci,DHT11_Data.humi_int,DHT11_Data.humi_deci,ppm,result);
			OneNet_Publish(pubtopics, pubbud);
			Delay_ms(5000);
			result=0;
		}
		else{
		Angle=0;
			Servo_SetAngle(Angle);
		}
		if (Serial_RxFlag == 1)		//如果接收到数据包
		{
			
			if (strcmp(Serial_RxPacket, "ON") == 0)			
			{
				Angle = 30;	
				Servo_SetAngle(Angle);				
				result=1;			
			Servo_SetAngle(Angle);
			Serial_Printf("angle值: %d\r\n", result);	
			UsartPrintf(USART_DEBUG, "OneNet_Publish\r\n");
			sprintf(pubbud,"{\"tem\": %d.%d,\"hum\": %d.%d,\"ppm\": %d,\"servo\": %d}",DHT11_Data.temp_int,DHT11_Data.temp_deci,DHT11_Data.humi_int,DHT11_Data.humi_deci,ppm,result);
			OneNet_Publish(pubtopics, pubbud);
			Delay_ms(5000);
			result=0;
				
			}
			else						//上述所有条件均不满足，即收到了未知指令
			{
				Serial_SendString("ERROR_COMMAND\r\n");			//串口回传一个字符串ERROR_COMMAND						
			}
			
			Serial_RxFlag = 0;			//处理完成后，需要将接收数据包标志位清零，否则将无法接收后续数据包
		}
		
		
		
		if(++timeCount >= 200)									//发送间隔5s
		{
			UsartPrintf(USART_DEBUG, "OneNet_Publish\r\n");
			sprintf(pubbud,"{\"tem\": %d.%d,\"hum\": %d.%d,\"ppm\": %d,\"servo\": %d}",DHT11_Data.temp_int,DHT11_Data.temp_deci,DHT11_Data.humi_int,DHT11_Data.humi_deci,ppm,result);
			OneNet_Publish(pubtopics, pubbud);
			
			timeCount = 0;
			ESP8266_Clear();
		}
		
		dataPtr = ESP8266_GetIPD(3);
		if(dataPtr != NULL)
			OneNet_RevPro(dataPtr);
		
		
		Delay_ms(10);
		
	}
	
	
	}

